/** @file render_window.h
    @brief Render window widget */
#ifndef RENDER_WINDOW_H
#define RENDER_WINDOW_H

#include <Ogre.h>
#include <QtGui/QWidget>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QShowEvent>
#include <QTimer>

#include "interface.hpp"
#include "logger.hpp"
#include "pixel/pixelworld.hpp"
#include "hud/text_overlay.hpp"

#include "frame_listener.hpp"

#ifdef Q_WS_X11
  #include <QtGui/QX11Info>
#endif

#ifdef _WIN32
    #include <windows.h>
#endif

QT_BEGIN_NAMESPACE

class RenderWindow: public QWidget {
    Q_OBJECT

public:
    RenderWindow( QWidget *p_parent = 0 );
    ~RenderWindow();

    //! Tools
    enum TOOL {
        //! Drawing tool
        T_DRAW = 0,
        //! Selection tool
        T_SELECT = 1,
        //! Move view tool
        T_MOVE = 2
    };

    /**
        @brief Initializes the render window
    */
    void init();

    /**
        @brief Gets a pointer to the frame listener
        @return Pointer
    */
    inline pm3::FRAME_LISTENER *get_framelistener() const {
        return m_frame_listener;
    }

    /**
        @brief Let Qt know we have a custom paint engine
        @note Needed when "WINDOW" handle is passed to QWidget::create. Without this, Qt keeps overpainting the window.
        @return NULL pointer
    */
    inline QPaintEngine *paintEngine () const {
        return 0;
    }

    /**
        @brief Selects a color for painting
        @param[in] r_color Reference to the color to draw with
    */
    void set_paint_color( const Ogre::ColourValue &r_color );

    /**
        @brief Gets current painting color
        @return Current color
    */
    inline Ogre::ColourValue get_paint_color() const {
        return m_color;
    }

    /**
        @brief Selects a tool
        @param[in] e_tool The tool to be selected
    */
    void set_tool( TOOL e_tool );

    /**
        @brief Gets current tool
        @return Current tool
    */
    inline TOOL get_tool() const {
        return m_tool;
    }

signals:
    void room_picked( pm3::PIXELROOM *p_room );

protected:
    void paintEvent( QPaintEvent *p_event );
    void resizeEvent( QResizeEvent *p_event );
    void showEvent( QShowEvent *p_event );

    void mousePressEvent( QMouseEvent *p_event );
    void mouseMoveEvent( QMouseEvent *p_event );
    void mouseReleaseEvent( QMouseEvent *p_event );

    void config_ogre();
    void init_listeners();
    void init_window();
    void init_ogre();

    void update();

    Ogre::Vector3 pick_grid( int i_x, int i_y, int i_z );

private:
    //! Update Timer
    QTimer *m_timer;

    //! Pointer to Ogre Root
    Ogre::Root* m_root;
    //! Pointer to the Ogre scene manager
    Ogre::PCZSceneManager* m_scene_manager;
    //! Pointer to the Ogre window of the render window widget
    Ogre::RenderWindow* m_window;
    //! Pointer to the camera used for the render window
    Ogre::Camera* m_cam;

    //! Pointer to text overlay on the render window
    pm3::hud::TEXT_OVERLAY *m_text_overlay;

    //! Pointer to frame listener
    pm3::FRAME_LISTENER *m_frame_listener;

    //! Selected color
    Ogre::ColourValue m_color;
    //! Selected tool
    TOOL m_tool;
};

QT_END_NAMESPACE

#endif // RENDER_WINDOW_H
